﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Blam_Unity.Classes.Engine.Halo_AA.gameAssetLevel.maps.images;
using System.IO;

namespace Blam_Unity.Forms.Halo_AA.gameAssetLevel.maps.images
{
    public partial class frmMapImageBLF : Form
    {
        public string fileName;

        bool isLarge = false;

        int originalImageSize;

        public frmMapImageBLF()
        {
            InitializeComponent();
        }

        private void frmMapImageBLF_Load(object sender, EventArgs e)
        {
            MojosIO mio = new MojosIO(fileName, Endian.Big);

            this.Text += fileName;

            originalImageSize = readMapImageBLF.getJIFILength(mio);
            byte[] jifiImage = readMapImageBLF.getJIFIImage(mio, originalImageSize);

            MemoryStream originalJIFIStream = new MemoryStream(jifiImage);
            Bitmap detectImageType = (Bitmap)Image.FromStream(originalJIFIStream);

            if (detectImageType.Width == 400 && detectImageType.Height == 160) { isLarge = true; }
            else { isLarge = false; }

            if (isLarge == true)
            {
                lblBLFTypes.Text = "BLF type: Normal\n\nWidth: 400\nHeight: 160\n\n";
                pBBLFImage.Size = new Size(new Point(400, 160));
                pBBLFImage.Image = detectImageType;
                lblBLFTypes.Location = new Point((12 + pBBLFImage.Width + 2), 24);
            }
            else
            {
                lblBLFTypes.Text = "BLF type: Small\n\nWidth: 229 \nHeight: 141\n\n";
                pBBLFImage.Size = new Size(new Point(229, 141));
                pBBLFImage.Image = detectImageType;
                lblBLFTypes.Location = new Point((12 + pBBLFImage.Width + 2), 24);
                gBMapImageProperties.Height -= 20;
                btnExtractCurrentImage.Location = new Point(12, 193);
                btnInjectImage.Location = new Point(388, 193);
            }
            mio.Flush();
            mio.Close();
        }

        private void btnExtractCurrentImage_Click(object sender, EventArgs e)
        {
            FileInfo fi = new FileInfo(this.Text.Replace("blf.mapimage - ", ""));

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "JPG Image File (*.jpg)|*.jpg|JPEG Image File (*.jpeg)|*.jpeg";
            sfd.FileName = fi.Name.Remove((fi.Name.Length - 4));
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                pBBLFImage.Image.Save(sfd.FileName);
            }
        }
    }
}
